package com.itheima.leetcode.od.b.sort;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <h3>智能成绩表</h3>
 * 小明来到某学校当老师，需要将学生按考试总分或单科分数进行排名，你能帮帮他吗? 输入描述 第 1 行输入两个整数，学生人数 n 和科目数量 m 0<n<100 0<m < 10 第 2 行输入 m 个科目名称，彼此之间用空格隔开， 科目名称只由英文字母构成，单个长度不超过10个字符。 科目的出现顺序和后续输入的学生成绩一一对应 不会出现重复的科目名称 第 3 行开始的 n 行，每行包含一个学生的姓名和该生 m 个科目的成绩 (空格隔开)。学生不会重名 学生姓名只由英文字母构成，长度不超过10个字符 成绩是0~100的整数，依次对应第2行种输入的科目 第n+2行，输入用作排名的科目名称。若科目不存在，则按总分进行排序. 输出描述 输出一行，按 成绩排序后的学生名字，空格隔开。成绩相同的按照学生姓名字典顺序排序
 * <p>
 * 示例1：
 * <p>
 * 输入
 * <p>
 * 3 2
 * <p>
 * yuwen shuxue
 * <p>
 * fangfang 95 90 xiaohua 88 95 minmin 100 82
 * <p>
 * shuxue
 * <p>
 * 输出
 * <p>
 * xiaohua fangfang minmin
 * <p>
 * 说明 按shuxue成绩排名，依次是xiaohua、fangfang、minmin
 */
public class SmartScoreTable {
    public static void main(String[] args) {
        /*Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        String[] study = new String[m];
        for (int i = 0; i < m; ++i) {
            study[i] = in.next();
        }

        List<Student> all_student = new ArrayList<>();
        //family relationship
        for (int i = 0; i < n; i++) {
            String name = in.next();

            int[] scores = new int[m];
            int j = 0;
            int total_score = 0;
            while (true) {
                if (j >= m) {
                    break;
                } else {
                    scores[j] = in.nextInt();
                    total_score += scores[j];
                }
                j += 1;
            }

            Student student = new Student(name, scores, total_score);
            all_student.add(student);
        }

        String target_study = in.next();
        int target_idx = -1;
        for (int i = 0; i < m; i++) {
            if (target_study.equals(study[i])) {
                target_idx = i;
                break;
            }
        }
        for (int i = 0; i < all_student.size(); i++) {
            all_student.get(i).idx = target_idx;
        }

        Collections.sort(all_student);*/

        int n = 3;
        int m = 2;
        String[] study = "yuwen shuxue".split(" ");
        String input = "fangfang 95 90\nxiaohua 88 95\nminmin 100 82";
        String targetStudy = "shuxue";

        List<Student> students = Arrays.stream(input.split("\n"))
                .map(str -> str.split(" "))
                .map(arr -> {
                    int[] stuScores = Arrays.stream(arr)
                            .skip(1)
                            .mapToInt(Integer::parseInt)
                            .toArray();
                    int total = Arrays.stream(stuScores)
                            .sum();
                    return new Student(arr[0], stuScores, total);
                })
                .collect(Collectors.toCollection(ArrayList<Student>::new));

        System.out.println(getResult(students, study, targetStudy));
    }

    public static String getResult(List<Student> students, String[] study, String targetStudy) {
        int targetIdx = -1;
        for (int i = 0; i < study.length; i++) {
            if (targetStudy.equals(study[i])) {
                targetIdx = i;
                break;
            }
        }

        int finalTargetIdx = targetIdx;

        return students.stream()
                .map(student -> {
                    student.idx = finalTargetIdx;
                    return student;
                })
                .sorted((o1, o2) -> {
                    int idx = o1.idx;
                    if (-1 != idx) {
                        if (o1.scores[idx] != o2.scores[idx]) {
                            return o2.scores[idx] - o1.scores[idx];
                        } else {
                            return o2.name.compareTo(o1.name);
                        }
                    } else {
                        if (o1.totalScore != o2.totalScore) {
                            return o2.totalScore - o1.totalScore;
                        } else {
                            return o2.name.compareTo(o1.name);
                        }
                    }
                })
                .map(student -> student.name)
                .collect(Collectors.joining(" "));
    }

    public static class Student {
        String name;
        int[] scores;
        int totalScore;
        int idx;

        public Student(String name, int[] scores, int totalScore) {
            this.name = name;
            this.scores = scores;
            this.totalScore = totalScore;
        }
    }
}